package com.etong.frame.directive.impl.database;

import java.util.Map;

import com.etong.frame.directive.util.CommonStatus;

public class DatabaseSelect extends DatabaseDirective {

	String selectBuilder;

	public DatabaseSelect(Statement statement) {
		super(statement);
	}

	public String getStatementConfig() {
		return selectBuilder;
	};

	@Override
	public boolean buildConst(Statement op) {

		String whereExpr = getStatement().getWhere_expr();
		String groupExpr = getStatement().getGroup_expr();
		String orderExpr = getStatement().getOrder_expr();

		StringBuilder builder = new StringBuilder(256);
		builder.append("Select ").append(getStatement().getTable_fields())
				.append(" from ").append(getStatement().getTable_name());

		if (null != whereExpr && !whereExpr.isEmpty()) {
			builder.append(" where ").append(whereExpr);
		}

		if (null != groupExpr && !groupExpr.isEmpty()) {
			builder.append(" group by ").append(groupExpr);
		}

		if (null != orderExpr && !orderExpr.isEmpty()) {
			builder.append(" order by ").append(orderExpr);
		}

		selectBuilder = builder.toString();

		// 创建Select语句变量
		addPlaceholders(createPlaceholders(selectBuilder, true));

		logger.info("Build select:" + getStatement().getName() + "->"
				+ selectBuilder);
		return true;
	}

	@Override
	protected CommonStatus buildVariable(Map<String, Object> vars) {
		CommonStatus status = new CommonStatus();
		// 设置查询sql语句
		status.setObject(resovlePagination(vars, selectBuilder));
		return status;
	}
}
